package com.zkthink.ceres.tenant.controller;


import static com.zkthink.ceres.tenant.enumeration.TenantStatusEnum.NORMAL;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zkthink.base.R;
import com.zkthink.base.controller.SuperCacheController;
import com.zkthink.ceres.authority.entity.auth.User;
import com.zkthink.ceres.authority.service.auth.UserService;
import com.zkthink.ceres.tenant.dto.TenantStatusDTO;
import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.log.annotation.SysLog;
import com.zkthink.ceres.tenant.dto.TenantSaveDTO;
import com.zkthink.ceres.tenant.dto.TenantUpdateDTO;
import com.zkthink.ceres.tenant.entity.Tenant;
import com.zkthink.ceres.tenant.enumeration.TenantStatusEnum;
import com.zkthink.ceres.tenant.service.TenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器 企业
 * </p>
 *
 * @author ceres
 * @date 2019-10-24
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/tenant")
@Api(value = "Tenant", tags = "商家管理")
@SysLog(enabled = false)
public class TenantController extends
    SuperCacheController<TenantService, Long, Tenant, Tenant, TenantSaveDTO, TenantUpdateDTO> {

  @Autowired
  private UserService userService;

  @ApiOperation(value = "查询所有商家", notes = "查询所有商家")
  @GetMapping("/all")
  public R<List<Tenant>> list() {
    return success(baseService.list(Wraps.<Tenant>lbQ().eq(Tenant::getStatus, NORMAL)));
  }

  @Override
  public R<Tenant> handlerSave(TenantSaveDTO model) {
    Tenant tenant = null;
    try {
      tenant = baseService.save(model);
    } catch (Exception e) {
      log.error("保存商家异常:{}", e);
      return R.fail(e.getMessage());
    }
    return success(tenant);
  }

  @Override
  public R<Tenant> handlerUpdate(TenantUpdateDTO model){
    Tenant tenant = null;
    try {
      tenant = baseService.update(model);
    } catch (Exception e) {
      log.error("保存商家异常:{}", e);
      return R.fail(e.getMessage());
    }
    return success(tenant);
  }

  @Override
  public void handlerResult(IPage<Tenant> page) {
    for(Tenant tenant:page.getRecords()){
      List<User> userList = userService.getByTenantCode(tenant.getCode());
      if(userList!=null && userList.size()>0){
        tenant.setAccount(userList.get(0).getAccount());
      }
    }
  }

  @ApiOperation(value = "检测商家是否存在", notes = "检测商家是否存在")
  @GetMapping("/check/{code}")
  @ApiOperationSupport(author = "ceres")
  public R<Boolean> check(@PathVariable("code") String code) {
    return success(baseService.check(code));
  }

  @Override
  public R<Boolean> handlerDelete(List<Long> ids) {
    // 这个操作相当的危险，请谨慎操作！！
    return success(baseService.delete(ids));
  }

  @ApiOperationSupport(author = "ceres")
  @ApiOperation(value = "修改商家状态", notes = "修改商家状态")
  @PutMapping("/status")
  public R<Boolean> updateStatus(@RequestBody TenantStatusDTO tenantStatusDTO) {
    return success(baseService
        .update(Wraps.<Tenant>lbU().set(Tenant::getStatus, tenantStatusDTO.getStatus()).in(Tenant::getId, tenantStatusDTO.getIds())));
  }
}
